﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SER = CPPEI.ETL.Domain.Services;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using CPPEI.ETL.Domain;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// 获取第三方数据表的列信息
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("TPT", "GetColumns", "1.0")]
    public class GetColumnsAction : BaseAction<GetColumnsParams, GetColumnsResult>
    {
        protected override ActionResult<GetColumnsResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetColumnsParams opParams)
        {
            GetColumnsResult result = new GetColumnsResult();

            ListResult<Domain.Data.ThirdPartyTableColumn> serResult = new SER.DtTableService().GetThirdPartyColumns(opParams.TableId);
            if (serResult.IsSuccess)
            {
                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetColumnsResult.Item item = new GetColumnsResult.Item();
                    item.Name = serResult.Data[index].Name;
                    item.Index = serResult.Data[index].Index;
                    item.Type = serResult.Data[index].Type;
                    item.Length = serResult.Data[index].Length;
                    item.IsNullable = serResult.Data[index].IsNullable;
                    item.PK = serResult.Data[index].PK;

                    result.Items.Add(item);
                }
                return new ActionResult<GetColumnsResult>(result);
            }
            else
            {
                return new ActionResult<GetColumnsResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetColumnsParams
    {
        public int TableId { get; set; }
    }

    public class GetColumnsResult
    {
        public List<Item> Items { get; set; }

        public GetColumnsResult()
        {
            Items = new List<Item>();
        }

        public class Item
        {
            public string Name { get; set; }
            public int Index { get; set; }
            public string Type { get; set; }
            public int Length { get; set; }

            public bool IsNullable { get; set; }

            public bool PK { get; set; }
        }
    }
}
